package Project4;

import java.util.*;

class ExpressionAssignment extends Statement{
	ArrayList<String> expression = new ArrayList<String>();
	Hashtable<String, Variable> variables;
	String variableToSave;
	boolean containsVars = false;

	public ExpressionAssignment(String s, Hashtable<String, Variable> v){
		variableToSave = s;
		variables = v;
	}

	public void add(String s){
		expression.add(s);
	}

	public int getValue(){
		String[] arr = new String[expression.size()];
		arr = expression.toArray(arr);
		return Tools.evaluateFixed(arr);
	}

	public String toString(){
		int registers = 1;
		String register = "$t1";
		String r = "#-- Assignment statement of " + variableToSave + "--\n";

		if(containsVars){

		}
		else{
			r += "li " + register + ", " + this.getValue() + "\n";
		}
		r += "li $t0, " + variables.get(variableToSave).getOffset()*4 + "\n";
		r += "add $t0, $s0, $t0 #move to variable's memory location \n";
		r += "sw " + register + ", ($t0)\n";
		r += "#-- End assignment -- \n\n";
		return r;
	}

	public void printExpression(){
		String[] arr = new String[expression.size()];
		arr = expression.toArray(arr);

		System.out.println("li $t0, " + Tools.evaluateFixed(arr));
		System.out.println(variableToSave + " = $t0");
	}
}
